var url = serverPre + "/manager/scs/v2/management/template";

var bsConf = {

    queryFormId: "template-query-form",

    createFormId: "template-create-form",

    tableID: "id",

    queryCols: 3,

    queryFloat: "right",

    voName: "template",
    initData: true,
    // needChangeTab: true,
    //
    // needInitECharts: true,

    queryConfigs:                   //页面头部查询form
        [
            {
                type: 'text',
                name: 'templateNo',
                placeholder:'模板编号',
                label:'模板编号',
                default: '',
                verify: ''
            },
            {
                type: 'text',
                placeholder: '模板名称',
                label: '模板名称',
                name: 'templateName',
                default: '',
                verify: ''
            },
            {
                type: 'select',
                placeholder: '签章类型',
                label: '签章类型',
                name: 'sealType',
                cols: [["",""], [1, '私章'],[2, '公章'],[3,'公私章']]
            },
            {
                type: 'select',
                placeholder: '模板状态',
                label: '模板状态',
                name: 'templateStatus',
                cols: [["",""], [0, '停用'],[1, '启用']]
            },
            {
                type: 'select',
                placeholder: '审核状态',
                label: '审核状态',
                name: 'auditStatus',
                cols: [["",""], [0, '待审核'],[1, '审核中'],[2,'已开通'],[3,'审核拒绝'],[4,'申请撤回']]
            },
            {
                type: 'text',
                placeholder: '创建人',
                label: '创建人',
                name: 'createName',
                default: '',
                verify: ''
            }
            ,
            {
                type: 'datetimerange',
                placeholder: '创建时间',
                label: '创建时间',
                name: 'timeRange',
                default: '',
                verify: ''
            },
            {
                type: 'select',
                id :'zh',
                placeholder: '租户',
                label: '租户',
                name: 'companyId',
                idProperty:"companyId",
                verify:"companyId_zh",
                dataUrl:serverPre+"/manager/scs/v2/company",
                nameProperty: 'companyAbbrName'
            },
            {
                type: 'select',
                placeholder: '产品',
                label: '产品',
                id:"cp",
                name: 'code',
                idProperty:"code",
                dataUrl:serverPre+"/manager/scs/v2/product",
                nameProperty: 'productName'
            },
            {
                type: 'select',
                placeholder: '管理员',
                label: '管理员',
                id:"gly",
                name: 'userId',
                idProperty:"userId",
                dataUrl:serverPre+"/manager/scs/v2/product/user",
                nameProperty: 'userName'
            }


        ],

    //列表内容
    tableCols: [
        {type: 'checkbox', width: 50},
        {field: 'id', title: '序号', type: 'numbers'}
        , {field: 'tenant', width: 185, title: '租户', align: 'center'}
        , {field: 'channelName', width: 100, title: '产品', align: 'center'}
        , {field: 'sealPerson', width: 100, title: '管理员', align: 'center'}
        , {field: 'templateNo', width: 155, title: '编号', align: 'center'}
        , {field: 'templateName', width: 300, title: '模板名称', align: 'center'}
        , {field: 'sealType', width: 110, title: '签章类型', align: 'center'}
        , {field: 'auditStatus', width: 140, title: '审核状态', align: 'center'}
        , {field: 'templateStatus', width: 100, title: '状态', align: 'center'}
        , {field: 'createUser', width: 150, title: '创建人', align: 'center'}
        , {field: 'createTime', width: 160, title: '创建时间', align: 'center'}
        , {field: 'updateUser',width: 150, title: '最后一次修改人', align: 'center'}
        , {field: 'updateTime',width: 160, title: '修改时间', align: 'center'}
        , {field: 'version',width: 100, title: '版本', align: 'center'}
        , {field: 'vip',width: 100, title: '级别', align: 'center'}
        , {fixed: 'right', width: 320, title: '操作', align: 'left', toolbar: '#bar'}
    ],

    bindOwnTableEventCol: function (layEvent, data) {
        if (layEvent === 'disable') { //禁用
            sealStatus(data['id'],2);
        } else if (layEvent === 'enable') { //启用
            sealStatus(data['id'],1);
        } else if (layEvent === 'view') {//预览
            var id = data['id'];
            xadmin.open("预览合同","../../template/look.html?id="+id,750,700);
        } else if (layEvent === 'allow'){
            //审批
            xadmin.open("审批","../../approval/serviceApprove.html?act=approve&id="+data['approvalId'],750,700)
        } else  if(layEvent === 'download'){
            //xadmin.open("审批","../../approval/serviceApprove.html?act=approve&id=1",800,800);
            refId = data['refId'];
            if(refId !== null && refId !== ""){
                exportTemplate(refId);
            }else {
                errorMsg("模板文件不存在！")
            }
        }else if (layEvent === 'beVip') { //启用
            // beVIP(data['id'], 1);
            var id = data['id'];
            xadmin.open("设置VIP通道","./setVip.html?id="+id,500,330);
        }else if (layEvent === 'notVip') { //启用
            beVIP(data['id'], 0);
        }
    }
}
function newTemplate() {
    xadmin.open("新增模板","./add.html",600,650);
}
function createAudit() {
    var checkStatus = table.checkStatus(bsConf.tableID);
    var templateArray = checkStatus.data;
    if(templateArray.length === 0) {
        errorMsg("请选择要配置审批流的模板！");
        return false;
    }
    if(templateArray.length > 1) {
        errorMsg("请选择一个模板进行配置！");
        return false;
    }
    var id = templateArray[0].id;
    xadmin.open("配置审批流","./audit.html?id="+id,650,720);
}

function  beVIP(a,b) {
    if(b === 1){
        var msg = "确定要设置为vip模板吗？";
    }else {
       var  msg = "确定要取消vip模板吗？";
    }
    layer.confirm(msg, {icon: 3, title:'修改状态'}, function(index){
        var u = serverPre + "/manager/scs/v2/management/template/vip/"+a+"/"+b;
        $.ajax({
            type: "GET",
            headers: {"token": getCookie("token")},
            url: u,
            success: function(data){
                setTimeout('window.location.reload()',500);
            }
        });
    });

}

function sealStatus(a,b) {
    if(b === 2){
        var msg = "确定要停用吗？";
    }else {
        var  msg = "确定要启用吗？";
    }
    layer.confirm(msg, {icon: 3, title:'修改状态'}, function(index){
        var u = serverPre + "/manager/scs/v2/management/template/status/"+a+"/"+b;
        $.ajax({
            type: "GET",
            headers: {"token": getCookie("token")},
            url: u,
            success: function(data){
                setTimeout('window.location.reload()',500);
            }
        });
    });

}

function query_search() {
    if ($("#up").hasClass("layui-icon-down")) {
        document.getElementById('up').classList.add("layui-icon-up");
        document.getElementById('up').classList.remove("layui-icon-down");
    }else{
        document.getElementById('up').classList.add("layui-icon-down");
        document.getElementById('up').classList.remove("layui-icon-up");

    }
    $("#product-query-form").toggle()
}
function  sealInfo(id) {
    debugger
    var u = serverPre + "/manager/scs/v2/seal/channel/sealinfo/"+id;
    var response = $.ajax({
        type: "GET",
        url: u,
        headers: {"token": getCookie("token")},
        async:false,
        success: function(data){
            debugger;
            //  alert(JSON.stringify(data)
        }
    });

    var json = response.responseJSON;

    if(json.code === 0){
        layer.open({
            type: 1,
            title: '客户产品信息',
            shadeClose: true,
            shade: false,
            maxmin: true,
            content: generateHtml(json.data),
            area: ['50%', '50%'],
            success: function () {

            }
        });
    }else{
        layer.msg("内部错误", {
            icon: 0,
            time: 2000
        });
    }
}
function exportTemplate(ids) {
    var url = serverPre + "/api/scs/v2/template/export/";
    window.location.href=url+ids;
    return false;
}




function generateHtml(data){
    return "   <div class=\"layui-row\" style=\"padding-top: 20px\">\n" +
        "            <div class=\"layui-col-xs8\">\n" +
        "                <div class=\"look-form-class\">\n" +
        "                    <form class=\"layui-form\" id=\"preViewForm\">\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">印章名称</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" id=\"sealName\"  value='"+data.sealName+"' readonly=\"readonly\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">印模编码</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" id=\"sealCode\" value='"+data.sealCode+"'  name=\"sealCode\" lay-verify=\"title\" readonly=\"readonly\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">印章密码</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" id=\"sealPwd\" value='"+data.sealPwd+"' name=\"sealPwd\" lay-verify=\"title\" readonly=\"readonly\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">证件类型</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" id=\"identificationTypeName\" value='"+data.identificationTypeName+"' name=\"identificationTypeName\" lay-verify=\"title\" readonly=\"readonly\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">证件号码</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" name=\"identificationNo\" value='"+data.identificationNo+"' id=\"identificationNo\" readonly=\"readonly\" lay-verify=\"title\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">生成时间</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" name=\"createTime\" id=\"createTime\"  value='"+data.createTime.substring(0,10)+"' readonly=\"readonly\" lay-verify=\"title\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <div class=\"layui-input-block\" style=\"padding-top:20px;padding-left: 150px\">\n" +
        "                                <button class=\"layui-btn layui-btn-primary\" id=\"closeWin\">关闭</button>\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                    </form>\n" +
        "                </div>\n" +
        "            </div>\n" +
        "            <div class=\"layui-col-xs3\">\n" +
        "                <div class=\"look-image-form-class\">\n" +
        "                        <div style=\"padding-top: 100px;padding-left: 25px\">\n" +
        "                            <img src=\"data:image/jpeg;base64,"+data.image+"\" style=\"width:157px;height:157px\">\n" +
        "                        </div>\n" +
        "                </div>\n" +
        "            </div>\n" +
        "        </div>\n" +
        "    </div>"
}

